# Maintainer:  Sergej Pupykin <arch+pub@sergej.pp.ru>
# Maintainer:  Eli Schwartz <eschwartz@archlinux.org>
# Contributor: TJ Vanderpoel <tj@rubyists>

pkgname=musl
pkgver=1.2.4
pkgrel=1
pkgdesc='Lightweight implementation of C standard library'
arch=('loong64' 'x86_64')
url='https://www.musl-libc.org/'
license=('MIT')
options=('staticlibs' '!buildflags')
validpgpkeys=('836489290BB6B70F99FFDA0556BCDB593020450F')
source=(https://www.musl-libc.org/releases/musl-$pkgver.tar.gz{,.asc}
        0001-musl-add-loongarch64-support.patch)
sha256sums=('7a35eae33d5372a7c0da1188de798726f68825513b7ae3ebe97aaaa52114f039'
            'SKIP'
            '3c49faf5ec25f2b0591f5d6c2ea73d11f3257802e696493a45d1293d85108ccb')

prepare() {
	cd "$pkgname-$pkgver"
	patch -p1 -i "$srcdir/0001-musl-add-loongarch64-support.patch"
}

build() {
  cd $pkgname-$pkgver
  ./configure --prefix=/usr/lib/musl \
    --exec-prefix=/usr \
    --enable-wrapper=all \
    CFLAGS="-ffat-lto-objects"
  make
}

package() {
  cd $pkgname-$pkgver
  make DESTDIR="$pkgdir" install

  # configure syslibdir with /lib for PT_INTERP compat, but install to /usr/lib
  mv "$pkgdir"/lib/ld-musl*.so* "$pkgdir"/usr/lib/
  rmdir "$pkgdir"/lib

  install -Dm0644 README "$pkgdir"/usr/share/doc/musl/README
  install -Dm0644 COPYRIGHT "$pkgdir"/usr/share/licenses/musl/COPYRIGHT
}
